﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
using System.Text;
using Exam.Model;
using System.Threading.Tasks;

namespace Exam.Service
{

    public class Repository<T> : IRepository<T> where T : class
    {
        private readonly Exam.DBManager.IDbManager m_Manager;
        public Repository(Exam.DBManager.IDbManager manager)
        {
            m_Manager = manager;
        }

        #region 查询
        public List<T> GetAll()
        {
            return m_Manager.Exam.Set<T>().AsNoTracking().ToList();
        }

        public T GetById(int id)
        {
            return m_Manager.Exam.Set<T>().Find(id);
        }
        #endregion

        public List<T> GetInclude(params Expression<Func<T, object>>[] includes)
        {
            var query = m_Manager.Exam.Set<T>().AsNoTracking();
            if (includes != null)
            {
                query = includes.Aggregate(query, (current, include) => current.Include(include));
            }
            return query.ToList();
        }

        public List<T> GetCondition(params Expression<Func<T, bool>>[] whereLambda)
        {
            var list = m_Manager.Exam.Set<T>().AsNoTracking();
            if (whereLambda != null)
            {
                foreach (var item in whereLambda)
                {
                    list = list.Where(item);
                }
            }
            return list.ToList();
        }

        public List<T> GetConditionOederby<Tkey>(Expression<Func<T, bool>> whereLambda, bool isAsc, Expression<Func<T, Tkey>> orderbyLambda)
        {
            var list = m_Manager.Exam.Set<T>().AsNoTracking().Where(whereLambda);
            if (isAsc)
            {
                list = list.OrderBy<T, Tkey>(orderbyLambda);
            }
            else
            {
                list = list.OrderByDescending<T, Tkey>(orderbyLambda);
            }

            return list.ToList();
        }

        #region 添加
        public T Add(T entity)
        {
            m_Manager.Exam.Set<T>().Add(entity);
            m_Manager.Exam.SaveChanges();
            return entity;
        }
        public bool AddOk(T entity)
        {
            m_Manager.Exam.Set<T>().Add(entity);
            return m_Manager.Exam.SaveChanges() > 0;
        }
        #endregion

        #region 修改
        public T Update(T entity)
        {
            m_Manager.Exam.Set<T>().Attach(entity).State = EntityState.Modified;
            m_Manager.Exam.SaveChanges();
            return entity;
        }

        public bool UpdateOk(T entity)
        {
            m_Manager.Exam.Set<T>().Attach(entity).State = EntityState.Modified;
            return m_Manager.Exam.SaveChanges() > 0;
        }

        #endregion

        #region 删除
        public bool Delete(int id)
        {
            var entity = m_Manager.Exam.Set<T>().Find(id);
            m_Manager.Exam.Set<T>().Remove(entity);
            return m_Manager.Exam.SaveChanges() > 0;
        }
        #endregion
    }
}
